
package commands;

import application.Core;

import java.io.File;

/**
 *	This class implements command ls.
 * 
 * @author Michal Mourek
 */
public class Ls extends AbstractCommand{
	
	/**
	 * Main method of this program.
	 * It print list directories and files in the specified directory.
	 *
	 * @param parameters List of parameters entered by user in command line.
	 * @param arguments List of arguments entered by user in command line.
	 * @param id Process ID.
	 * @param core reference to the core
	 * @author Michal Mourek
	 */
	@Override
	public void main(String[] parameters, String[] arguments, int id, Core core) {
		String workingFolder = core.getDirName(id);
		File verifyPath = new File(workingFolder);
		if (parameters.length == 0){
			if (arguments.length == 0){
				String[] arrayString = verifyPath.list();
				for (String string : arrayString) {
					core.write(id, string);
					try {
						Thread.sleep(50);
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
			} else{	
				File path = new File(arguments[0]);
				if (path.isDirectory()){
					String[] arrayString = path.list();
						for (String string : arrayString) {
							core.write(id, string);
							try {
								Thread.sleep(50);
							} catch (InterruptedException e) {
								// TODO Auto-generated catch block
								e.printStackTrace();
							}
						}
				}else {
					String[] arrayString = invalidArguments().split("\n");
						for (String string : arrayString) {
							core.write(id, string);
							try {
								Thread.sleep(50);
							} catch (InterruptedException e) {
								// TODO Auto-generated catch block
								e.printStackTrace();
							}
						}
				}
			}
		} else {
			for (int i = 0; i < parameters.length; i++) {
				char parameter = parameters[i].charAt(1);
				if (parameter == 'h'){
					String[] arrayString = help().split("\n");
					for (String string : arrayString) {
						core.write(id, string);
						try {
							Thread.sleep(50);
						} catch (InterruptedException e) {
							e.printStackTrace();
						}
					}
				} else{
					String[] arrayString = invalidParameters().split("\n");
					for (String string : arrayString) {
						core.write(id, string);
						try {
							Thread.sleep(50);
						} catch (InterruptedException e) {
							e.printStackTrace();
						}
					}
					break;
				}
			}
		}
	}
	
	/**
	 * Print help for this command.
	 *
	 * @author Michal Mourek
	 */
	@Override
	public String help() {
		return ("NAME: ls - print list files and directories \n SYNOPSYS: ls [OPTION] \n \n Description: \n   -h      print help \n");
		
		//throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
	}
	
	/**
	 * Print warning message, when parameter is invalid.
	 *
	 * @author Michal Mourek
	 */
	public String invalidParameters(){
		return ("Parameters is not correct \n Read help ls - h \n");
	}
	
	/**
	 * Print warning message, when argument is invalid.
	 *
	 * @author Michal Mourek
	 */
	public String invalidArguments(){
		return ("Arguments is not correct \n Read help ls - h \n");
	}

	
}
